package com.example.my_android

import android.os.Bundle
//import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
//import androidx.core.view.ViewCompat
//import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {

    private var isClicked = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        //布局
        val linearLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(50, 20, 50, 20)
        }
        //图片
        val imageView = ImageView(this@MainActivity).apply {
            setImageResource(R.drawable.flag1)
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = 30
                bottomMargin = 20
            }
        }
        linearLayout.addView(imageView)
        //文本框
        val textView = TextView(this@MainActivity).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply{
                topMargin = 200
            }
            gravity = android.view.Gravity.CENTER
            textSize = 20f
            text = getString(R.string.textview)
        }
        linearLayout.addView(textView)
        //按钮
        val button = Button(this@MainActivity).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = 300
            }
            text = getString(R.string.button)
            textSize = 20f

        }
        linearLayout.addView(button)

        setContentView(linearLayout)

        //按钮事件
        button.setOnClickListener {
            if (isClicked) {
                button.text = getString(R.string.button)
                isClicked = false
            } else {
                button.text = getString(R.string.Clicked_button)
                isClicked = true
            }
        }
    }
}